﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zarzadzanie_Restautacja.Model
{
    [Table("Zamowienia", Schema = "dbo")]
    public class Zamowienie
    {
        #region Properties

        [Column("Id"), DatabaseGenerated(DatabaseGeneratedOption.None), Key]
        public int Id { set; get; }
        [Column("StatusPot")]
        public int Status { get; set; }
        [Column("Cena")]
        public decimal Cena { get; set; }
        [Column("NrStolika")]
        public int NrStolika { get; set; }
        [Column("Data")]
        public DateTime data { get; set; }

        [NotMapped]
        public List<Potrawa> Potrawy {
            get
            {
                using (RestauracjaContext context = new RestauracjaContext())
                {

                    var combo = (from zp in context.ZamowieniaPotrawy
                                 from p in context.Potrawy
                                 where zp.IdZamowienia == this.Id &&
                                       p.Id == zp.IdPotrawy
                                 select new
                                 {
                                     Cena = p.Cena,
                                     Id = p.Id,
                                     Nazwa = p.Nazwa
                                 }).ToList();

                  return combo.Cast<Potrawa>().ToList();
                }
            } 
         }

        #endregion Properties

        public static int PobierzWolneId()
        {
            using (RestauracjaContext context = new RestauracjaContext())
            {
                if (context.Zamowienia.Any())
                {
                    return context.Zamowienia.Max(zam => zam.Id) + 1;
                }
                else
                {
                    return 0;
                }
            }
        }
    }
}
